/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package game.Rendering;

import game.Audio.AudioResources;
import game.Helper;
import game.Player.Input;
import game.Player.Player;
import game.Player.PlayerSkins;
import game.Scene;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * @author Filippo-TheAppExpert
 */
public class GamePanel extends Canvas implements Runnable {
    private Thread gameThread;
    private boolean isRunning;

    private int FPS = 0;

    public GamePanel() {
        //Helper.gamePanel = this;
        super.resolution = this.getBounds();

        setPreferredSize(new Dimension(Helper.GAME__WIDTH, Helper.GAME__HEIGHT));
    }


    private void init() throws Exception {
        PlayerSkins.init(); // Load player skins
        AudioResources.init(); // Load audios
        isRunning = true;

        try {
            //Scene mainScene = new Scene();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addNotify() {
        super.addNotify();
        if (gameThread == null) {
            gameThread = new Thread(GamePanel.this);
        }
        gameThread.start();
    }

    @Override
    public void run() {
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }

        long lastPrint = System.nanoTime();
        long lastUpdate = System.nanoTime();
        while (isRunning) {
            //long startTime = System.currentTimeMillis();

            long deltaTime = System.nanoTime() - lastUpdate;
            lastUpdate = System.nanoTime();

            FPS = (int) (1.0 / (deltaTime * 1e-9));
            update(deltaTime * 1e-9);
            renderGame();

            long endTime = System.nanoTime();
            long millisPassed = (endTime-lastPrint) / 1000000L;
            long waitTime = (1000L / Helper.FPS_LIMIT) - millisPassed;
            lastPrint = endTime;

            try {
                Thread.sleep(waitTime);

            } catch (Exception e) {

            }
        }
    }

    @Override
    protected void onKeyUp(KeyEvent event) {

    }

    @Override
    protected void onKeyPressed(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.VK_SPACE) {

        } else if (event.getKeyCode() == KeyEvent.VK_LEFT) {

        } else if (event.getKeyCode() == KeyEvent.VK_RIGHT) {

        } else {

        }
    }

    private void update(double deltaTime) {
        Input.update();
        if(Helper.mainScene != null) {
            Helper.mainScene.update(deltaTime);
        }

    }

    private void renderGame() {
        repaint();
        Toolkit.getDefaultToolkit().sync();
    }

    @Override
    protected void onDraw(Graphics2D g2D) {
        g2D.setColor(Color.red);

        if (Helper.mainScene != null && Helper.mainScene.getCamera() != null) {
            //Helper.mainScene.getCamera().draw(g2D);
        } else {

        }

        g2D.setColor(Color.black);
        g2D.drawString("FPS: " + FPS, 10, 30);
        if(Player.myPlayer != null) {
            g2D.drawString("Health: " + Player.myPlayer.getHealth(), 10, 60);
            //g2D.drawString("Ammo: " + Player.myPlayer.getBullets() + "/" + Player.myPlayer.getAmmo(), 10, 90);
        }
    }

    // MouseListener
    @Override
    public void mouseClicked(MouseEvent mouseEvent) {
        System.out.println("clicked");
    }

    @Override
    public void mousePressed(MouseEvent mouseEvent) {
        System.out.println("pressed");
    }

    @Override
    public void mouseReleased(MouseEvent mouseEvent) {
        System.out.println("released");
    }

    @Override
    public void mouseEntered(MouseEvent mouseEvent) {
        System.out.println("entered");
    }

    @Override
    public void mouseExited(MouseEvent mouseEvent) {
        System.out.println("exited");
    }
}
